package com.software.chemistry.service.Impl;

import com.software.chemistry.entity.SearchHistory;
import com.software.chemistry.repository.SearchHistoryRepository;
import com.software.chemistry.service.SearchHistoryService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class SearchHistoryServiceImpl implements SearchHistoryService {
    private final SearchHistoryRepository searchHistoryRepository;

    public SearchHistoryServiceImpl(SearchHistoryRepository searchHistoryRepository) {
        this.searchHistoryRepository = searchHistoryRepository;
    }

    @Override
    public List<SearchHistory> findByUserIdOrderByHistoryTimeDesc(String userId) {
        List<SearchHistory> searchHistories =  searchHistoryRepository.findByUserIdOrderByHistoryTimeDesc(userId);
        List<SearchHistory> list = new ArrayList<>();
        for (int i = 0; ( i < 6 && i < searchHistories.size()) ; i++){
            list.add(searchHistories.get(i));
        }
        return list;
    }

    @Override
    public void createStudentInfo(SearchHistory searchHistory) {
        searchHistoryRepository.save(searchHistory);
    }

    @Override
    public List<SearchHistory> findByUserId(String userId) {
        return searchHistoryRepository.findByUserId(userId);
    }


    @Override
    public void deleteByUserId(String userId) {
        searchHistoryRepository.deleteByUserId(userId);
    }


}
